package com.Offer;

/*
    面试题42：连续子数组的最大和
    题目描述：输入一个整型数组，数组中有正数也有负数，数组中一个或连续的多 个整数组成一个子数组，求连续子数组的最大和
 */
public class demo42 {
    public static void main(String[] args) {
        int[] input = {1,-2,3,10,-4,7,2,-5};
        System.out.println(maxSubArray(input));
        int[] child = {3,10,-4,7,2};
    }

    public static int maxSubArray(int[] nums){
        if(nums == null || nums.length == 0){
            return 0;
        }
        int sum = 0;
        int res = nums[0];
        for (int num : nums){
            sum = sum > 0 ? sum + num : num;
            System.out.println("sum："+sum);
            res = Math.max(sum,res);
            System.out.println("res:"+res);
        }
        return res;
    }
}
